<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

class GalleryModel extends JLModelBase {

    /**
     * Funcion que retorna Gallery
     * @Retorna array
     */
    public function getGalleryById($id) {
        $_true = null;

        $sql = "SELECT 
              `int_id_gallery`,
              `var_title_gallery`,
              `int_num_photos`,
              `var_path_gallery`,
              `int_id_sections_view`,
              `var_image_reference`,
              `int_id_state`
            FROM 
              `tbl_gallery` 
            WHERE int_id_gallery = " . $this->DB->Quote($id);

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = $this->DB->getDataArray();
            $_true = $_true[0];
        }

        return $_true;
    }

    /**
     * Actualiza Gallery
     * @Retorna bool
     */
    public function updateGallery($id, $var_title_gallery, $int_id_sections_view, $int_id_state, $bol_admin = 0) {
        $_true = false;

        $sql = "UPDATE 
              `tbl_gallery`  
            SET
              `var_title_gallery`       = " . $this->DB->Quote($var_title_gallery) . ",
              `int_id_sections_view`    = " . $this->DB->Quote($int_id_sections_view) . ",
              `int_id_state`            = " . $this->DB->Quote($int_id_state) . ",
              `bol_admin`               = " . $this->DB->Quote($bol_admin) . "
            WHERE 
              `int_id_gallery`          = " . $this->DB->Quote($id) . "";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        }

        return $_true;
    }

    /**
     * Actualiza Num Gallery
     * @Retorna bool
     */
    public function updateNumGallery($id, $num) {
        $_true = false;

        $sql = "UPDATE 
              `tbl_gallery`  
            SET
              `int_num_photos` = " . $this->DB->Quote($num) . "
            WHERE 
              `int_id_gallery` = " . $this->DB->Quote($id);

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        }

        return $_true;
    }

    /**
     * Inserta Gallery
     * @Retorna int
     */
    public function insertGallery($var_title_gallery, $int_num_photos, $var_path_gallery, $int_id_sections_view, $int_id_state, $bol_admin = 0) {
        $_true = 0;

        $sql = "INSERT INTO 
              `tbl_gallery`
            (
              `var_title_gallery`,
              `int_num_photos`,
              `var_path_gallery`,
              `int_id_sections_view`,
              `int_id_state`,
              `bol_admin`
            ) 
            VALUE (
              " . $this->DB->Quote($var_title_gallery) . ",
              " . $this->DB->Quote($int_num_photos) . ",
              " . $this->DB->Quote($var_path_gallery) . ",
              " . $this->DB->Quote($int_id_sections_view) . ",
              " . $this->DB->Quote($int_id_state) . ",
              " . $this->DB->Quote($bol_admin) . "   
            )";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = $this->DB->getInsertedId();
        }

        return $_true;
    }

    /**
     * Elimina Gallery
     * @Retorna bool
     */
    public function deleteGallery($id) {
        $_true = false;

        $sql = "DELETE FROM 
              `tbl_gallery` 
            WHERE 
              `int_id_gallery` = " . $this->DB->Quote($id) . " AND `int_id_state` <> 1";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        }

        return $_true;
    }

    public function getGalleriesBySectionId($id) {
        $sql = "SELECT 
        tbl_gallery.int_id_gallery,
        tbl_gallery.var_title_gallery,
        tbl_gallery.var_image_reference,
        tbl_sections.var_desc_section,
        tbl_sections.int_id_section
        FROM
        tbl_sections
        INNER JOIN tbl_gallery ON (tbl_sections.int_id_sections_view = tbl_gallery.int_id_sections_view)
        WHERE tbl_gallery.int_id_state <> 3 AND tbl_gallery.int_num_photos > 0 
            AND tbl_gallery.bol_admin = 0 AND  NOT ISNULL(tbl_gallery.var_image_reference) 
            AND tbl_sections.int_id_section = " . $this->DB->Quote($id) . "
        ORDER BY tbl_gallery.int_id_gallery DESC
        LIMIT 700";
        $this->DB->executeQuery($sql);
        return $this->DB->getDataArray();
    }

    public function getAllGalleries() {
        $sql = "SELECT 
        tbl_gallery.`int_id_gallery`,
        tbl_gallery.`var_title_gallery`,
        tbl_gallery.`var_image_reference`,
        tbl_sections.`var_desc_section`,
        IF(tbl_gallery.bol_admin, 1, tbl_sections.int_id_section) AS 'int_id_section'
        FROM `tbl_sections`
        INNER JOIN `tbl_gallery` ON (tbl_sections.int_id_sections_view = tbl_gallery.int_id_sections_view)
        WHERE `tbl_gallery`.`int_id_state` <> 3 AND `tbl_gallery`.int_num_photos > 0 AND NOT ISNULL(`tbl_gallery`.var_image_reference)
        ORDER BY tbl_gallery.int_id_gallery DESC
        LIMIT 700";
        $this->DB->executeQuery($sql);
        return $this->DB->getDataArray();
    }

    public function updateImageRefGallery($id, $ref = '') {
        $_true = false;

        $sql = "UPDATE 
              `tbl_gallery` 
            SET
              `var_image_reference` = '" . $ref . "'
            WHERE 
              `int_id_gallery` = " . $id;

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        }

        return $_true;
    }

    public function updateViewed($id) {
        $sql = "CALL sp_update_gallery_viewed(?)";
        $dbh = $this->getDbh();
        $sth = $dbh->prepare($sql);
        $sth->bindParam(1, $id, PDO::PARAM_INT, 11);
        $sth->execute();
    }

    public function countGalleriesBySectionViewId($id) {
        $sql = "SELECT 
            COUNT(`int_id_gallery`) AS 'num_gall'
        FROM 
            `tbl_gallery`
        WHERE `int_id_sections_view` = $id
        AND `int_id_state` <> 3 
        AND `bol_admin` = 0 AND `int_num_photos` > 0
        LIMIT 1";
        $this->DB->executeQuery($sql);
        $data = $this->DB->getDataArray();
        return $data[0];
    }

}